<?php

namespace app\api\controller;


use app\api\model\Area;
use app\common\controller\Api;

class Address extends Api
{

    protected $noNeedLogin = ['area','areas'];
    protected $noNeedRight = ['*'];


    public function area()
    {
        $data['provinceData'] = Area::where('level', 1)->order('id asc')->field('id as value, name as label, pid, level')->select();
        foreach ($data['provinceData'] as $k => $p) {
            $data['cityData'][$k] = Area::where(['level' => 2, 'pid' => $p->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            foreach ($data['cityData'][$k] as $i => $c) {
                $data['areaData'][$k][$i] = Area::where(['level' => 3, 'pid' => $c->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            }
        }

        $this->success('省市区', $data);

    }
    public function areas()
    {
        $level = $this->request->post('level',1);
        $pid = $this->request->post('pid',0);
        $data = Area::where(['level'=>$level,'pid'=>$pid])->order('id asc')->field('id, name, pid, level')->select();
        // $data['provinceData'] = Area::where('level', 1)->order('id asc')->field('id as value, name as label, pid, level')->select();
        // foreach ($data['provinceData'] as $k => $p) {
        //     $data['cityData'][$k] = Area::where(['level' => 2, 'pid' => $p->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
        //     foreach ($data['cityData'][$k] as $i => $c) {
        //         $data['areaData'][$k][$i] = Area::where(['level' => 3, 'pid' => $c->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
        //     }
        // }

        $this->success('省市区', $data);

    }


}